Docker Kata 4: Running a Web Server in a Container
Learn to run a web server in a container and view its default page.
We'll cover the following
Containers are useful as components of complex information systems. Web servers are, of course, an important component of any web-based application. This kata will demonstrate how containers can be used to run a web server.
Step 1: Run a web server#
The command to stop and remove all the containers is given below.
The output will be something like this:
Commands
Parameter | Description |
| This stops and removes all the containers. |
|
These commands stop and remove all the containers.
The command to run a disconnected NGINX container is given below.
When we execute the command above, the output will be something like this:
Commands
Parameter | Description |
| This is the parent command. |
| This runs a container. |
| This runs a container in disconnected mode. |
| This maps a TCP port from the host to the container. |
| This publishes a port from the container to the host. The format is |
| This assigns a name to a container. |
| This is the name assigned to the container. |
| This is the name of the image to run. |
Previous katas have used the NGINX container as a demonstration of a container that runs in disconnected mode. This kata shows an NGINX container doing what it’s designed to do: run an HTTP server.
Commands like this have been run in previous katas. The new parameter here is -p, which publishes a container port to a host port.
The command to view the NGINX default page is given below:
The result of the command above will be something like this:
Command
Parameter/Command | Description |
|
|
The curl program is a simple command-line HTTP client. This command uses curl to issue a request to the NGINX server from the command line.
The output will be something like this:
The last step demonstrates that the NGINX server can also be accessed by a web browser.
Step 2: Run a second web server on a different port#
The command to run a second disconnected NGINX server is given below.
The result of the command above after execution will be something like this:
Commands
Parameter | Description |
| This is the parent command. |
| This runs a container. |
| This runs a container in disconnected mode. |
| This maps a TCP port from the host to the container. |
| This publishes a port from the container to the host. The format is |
| This assigns a name to a container. |
| This is the name assigned to the container. |
| This is the name of the image to run. |
Both NGINX containers are now running, one mapped to host port 80, the other to host port 81. We should be able to use curl, alternating between ports 80 and 81, to view either web server. Port publishing, or mapping, can be used for a variety of use cases. Multiple web server containers can be run side by side on a single IP address, to distribute load, or to run different versions of the same application at the same time.
The command to view the NGINX default page is given below.
The output will be something like this:
Command
Parameter/Command | Description |
|
|
This command demonstrates that the second NGINX container is indeed running and mapped to host port 81. We can also use Firefox to view the page by visiting localhost:81.
Practice commands#
We’ve given a terminal and table containing a list of commands discussed in this lesson. Try out these commands after running the terminal, and check out the results!
Commands
Step | Command |
This runs a disconnected NGINX container named |
|
This views the NGINX default page using |
|
This runs a second disconnected NGINX server named |
|
This views the default page of the |
|
This stops and removes all the containers. |
|
/
Docker Kata 3: Container Volumes and File System
Docker Kata 5: Docker Networking